﻿using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// With LockInput, you can block the inputs from FingerInput, or others scripts that have inputs.
/// An object that wants to lock the input calls AddLock() with it's own LockType, then calls RemoveLock() to unlock.
/// All other objects check if the input is locked with Locked() or AvailableTo().
/// If it is locked, the input is cancelled (or other behaviour as needed).
/// </summary>
public class LockInput
{
	private static Dictionary<LockType, int> m_Locks = new Dictionary<LockType, int>();
	public enum LockType
	{
		None = -1,
		Player = 0,
		Environment = 1,
		Interactive = 2,
		HUD = 3,
		All,
	}
	
	/// <summary>
	/// Add a Lock to the dictionary
	/// </summary>
	/// <param name="i_Caller">The object type that is calling the lock</param>
	public static void AddLock(LockType i_Caller)
	{
		if (!m_Locks.ContainsKey(i_Caller))
		{
			m_Locks.Add(i_Caller, 0);
		}
		else
		{
			m_Locks[i_Caller] ++;
		}
	}
	
	/// <summary>
	/// Remove a Lock from the dictionary
	/// </summary>
	/// <param name="i_Caller">The object type that is calling the unlock</param>
	public static void RemoveLock(LockType i_Caller)
	{
		if (m_Locks.ContainsKey(i_Caller))
		{
			m_Locks[i_Caller] --;
			if (m_Locks[i_Caller] <= 0)
			{
				m_Locks.Remove(i_Caller);
			}
		}
	}

	/// <summary>
	/// Checks if the input is locked for a certain type
	/// </summary>
	/// <param name="i_Caller">The object type that the lock is compared to</param>
	/// <returns>Returns true is the current lock is made by the same type or if there are no locks</returns>
	public static bool AvailableTo(LockType i_Caller)
	{
		return (m_Locks.ContainsKey(i_Caller) && m_Locks.Count == 1) || m_Locks.Count == 0;
	}

	/// <summary>
	/// Checks if there is a lock in the dictionary
	/// </summary>
	/// <returns>Returns true if there is at least on lock in the dictionary</returns>
	public static bool Locked()
	{
		return m_Locks.Count > 0;
	}
}
